package org.fastsyncer.manager.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {

	public static boolean save(String filename, String data, boolean append) {
	    FileWriter writer = null;
	    try {
			// 保存文件内容
			writer = new FileWriter(filename, append);
			if(writer!=null){
			    writer.write(data);
			}
		} catch (Exception e) {
		} finally {
		    if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
		return true;
	}

	/**
	 * 递归删除目录下的所有文件及子目录下所有文件
	 * 
	 * @param dir
	 *            将要删除的文件目录
	 * @return boolean 如果返回true说明目录已被成功删除，如果false,这个方法还会尝试继续删除。
	 */
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			// 递归删除目录中的子目录下
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		return dir.delete();
	}

	public static StringBuffer read(String filename, int start, int size) {
	    StringBuffer r = new StringBuffer();
	    FileReader fr = null;
	    BufferedReader reader = null;
	    try {
		    fr = new FileReader(filename);
		    if(fr != null){
    		    reader = new BufferedReader(fr);
    			String line;
    			int i = 0;
    			int end = start + size;
    			while ((line = reader.readLine()) != null) {
    				if (start <= i && i < end) {
    					r.append(line);
    				}
    				if (i >= end) {
    					break;
    				}
    				i++;
    			}
		    }
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
            if(fr!=null){
                try {
                    fr.close();
                } catch (IOException e) {
                }
            }
        }
		return r;
	}

	public static int count(String filename) {
        File file = new File(filename);
        if (file==null || !file.exists()) {
            return 0;
        }
        
	    int count = 0;
	    FileInputStream fis = null;
	    InputStream is = null;
	    try {
            fis = new FileInputStream(filename);
            is = new BufferedInputStream(fis);
            byte[] c = new byte[1024];
            int readChars = 0;
            while ((readChars = is.read(c)) != -1) {
            	for (int i = 0; i < readChars; ++i) {
            		if (c[i] == '\n')
            			++count;
            	}
            }
            fis.close();
            is.close();
        } catch (Exception e) {
        } finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                }
            }
        }
	    return count;
	}

	public static void createFileIfNotExist(String fileName) {
		try {
			File file = new File(fileName);
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (IOException e) {
		}
	}

	public static void createDirIfNotExist(String fileDir) {
	    if(fileDir==null || fileDir == ""){
	        return;
	    }
	    
		// 检查文件夹是否创建
		File file = new File(fileDir);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

}